package cn.com.zero.file.config;

import cn.com.zero.file.client.MinioTemplate;
import cn.com.zero.file.config.properties.FileUploadProperties;
import cn.com.zero.file.constants.FileUploadConstant;
import cn.com.zero.file.service.IFileService;
import cn.com.zero.file.service.impl.MinioFileServiceImpl;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.constraints.NotEmpty;

/**
 * @author liuxh
 * @version 1.0.0
 * @Description minio配置类
 * @createTime 2022/9/13
 */
@Configuration
@EnableConfigurationProperties({FileUploadProperties.class})
@ConditionalOnProperty(name = "zero.file.type", havingValue = FileUploadConstant.FILE_TYPE_MINIO)
public class MinioConfig {

    @NotEmpty
    @Value("${zero.minio.endpoint}")
    private String endpoint;
    @NotEmpty
    @Value("${zero.minio.access-key}")
    private String accessKey;
    @NotEmpty
    @Value("${zero.minio.secret-key}")
    private String secretKey;

    /**
     * minio文件存储client
     */
    @Bean(name = "minioClient")
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    @Bean
    public MinioTemplate minioTemplate(MinioClient minioClient, FileUploadProperties fileUploadProperties) {
        return new MinioTemplate(minioClient, fileUploadProperties);
    }

    @Bean
    public IFileService fileService(MinioTemplate minioTemplate) {
        return new MinioFileServiceImpl(minioTemplate);
    }
}
